<?php
/**
 * com_easiv2
 * 
 * @author Lukas Gruber, Internet Technology Kapfenberg, FH JOANNEUN
 * @copyright 2011, Lukas Gruber, Internet Technology Kapfenberg, FH JOANNEUM
 * @license GNU/GPLv3 - http://www.gnu.org/licenses/gpl-3.0.html
 * @contact lukasgruber@gmx.net
 * @website http://www.lukasg255.bplaced.net
 */

defined('_JEXEC') or die();

jimport('joomla.application.component.controller');
require_once JPATH_COMPONENT.DS.'helpers'.DS.'ftphelper.php';
require_once JPATH_COMPONENT.DS.'helpers'.DS.'remotehelper.php';

class Easiv2ControllerManageInstances extends JController {
	
	var $remoteHelper;

	function __construct() {
		parent::__construct();
		
		$this->remoteHelper = new RemoteHelper();
	}
	
	function display() {
		JHTML::stylesheet('easiv2.css', JPATH_COMPONENT.DS.'css');

		parent::display();
	}
	
	function cancel() {
		JRequest::setVar('view', 'manageInstances');
		
		parent::display();
	}
	
	function gotoAddExistingInstance() {		
		JRequest::setVar('view', 'addExistingInstance');
		
		parent::display();
	}
	
	function gotoEditInstances() {
		JRequest::setVar('view', 'editInstances');
		
		parent::display();
	}
	
	function gotoExtensions() {
		JRequest::setVar('view', 'manageExtensions');
		
		parent::display();
	}
	
	/**
	 * Validation of user input and saving to database
	 */
	function saveInstance() {		
		$name = JRequest::getVar('name');
		$port = JRequest::getVar('port');
		$http_address = JRequest::getVar('http_address');
		$ftp_host = JRequest::getVar('ftp_host');
		$host_folder = JRequest::getVar('host_folder');
		$ftp_user = JRequest::getVar('ftp_user');
		$ftp_pass = JRequest::getVar('ftp_pass');
		$identifier = JRequest::getVar('identifier');
		
		/*
		 * check and modify userinput for correct syntax if nescessary
		 */
		if(substr($http_address, strlen($http_address)-1) != '/') {
			$http_address .= '/';
		}
		
		if(substr($http_address, 0, 7) != "http://") {
			$http_address = 'http://'.$http_address;
		}

		/*
		 * check for correct input of ftp host. Joomla! requires no 'ftp://' at the beginning and no '/' at the end
		 * Also 'localhost' does not work...
		 */
		//check delete a '/' at the end
		if(substr($ftp_host, strlen($ftp_host)-1) == '/') {
			$ftp_host = substr($ftp_host, 0, strlen($ftp_host)-1);
		}
		
		//remove 'ftp://'
		if(substr($ftp_host, 0, 6) == 'ftp://') {
			$ftp_host = substr($ftp_host, 6);
		}
		
		//turn 'localhost' into 127.0.0.1
		if($ftp_host == "localhost") {
			$ftp_host = "127.0.0.1";
		}
		
		if($ftp_user != "" && $ftp_pass != "" && $http_address != "" && $ftp_host != "") {
			/*
			 * Check, if Joomla! instance is responding
			 */
			$response = $this->remoteHelper->isInstanceResponding($http_address);
			
			if($response == "true") {
				$ftp = new FtpHelper();
				
				if($ftp->isFtpValid($ftp_host, $port, $ftp_user, $ftp_pass)) {
					/*
					 * Setting the identifier at remote instance
					 */
					$response = $this->remoteHelper->setIdentifier($http_address, 'null', $identifier);
					
					$db = JFactory::getDBO();
					$query = "INSERT INTO #__easiv2_instances VALUES (null, '".$name."', '".$http_address."', '".$ftp_host."', '".$port."', '".$host_folder."', '".$ftp_user."', '".$ftp_pass."', '".$identifier."');";
					$db->setQuery($query);
					$db->query();
					
					JRequest::setVar('view', 'manageInstances');
					
					echo "<img src=\"components/com_easiv2/images/true.png\" /> ".JText::_('COM_EASIV2_MANAGEINSTANCES_CONTROLLER_ADDEDSUCCESSFULLY')."<br>";
				}
				else {
					echo "<img src=\"components/com_easiv2/images/false.png\" /> ".JText::_('COM_EASIV2_MANAGEINSTANCES_CONTROLLER_FTPCONNECTIONFAILED')."<br>";
				}
			}
			else {
				echo "<img src=\"components/com_easiv2/images/false.png\" /> ".JText::_('COM_EASIV2_MANAGEINSTANCES_CONTROLLER_INSTANCECONNECTIONFAILED')."<br>";
			}
		}
		else {
			echo "<img src=\"components/com_easiv2/images/false.png\" /> ".JText::_('COM_EASIV2_MANAGEINSTANCES_CONTROLLER_INFORMATIONMISSING')."<br>";
		}
		
		parent::display();
	}
	
	/**
	 * Delete instances from database
	 */
	function deleteInstances() {
		$idToDelete = JRequest::getVar('cid');
		$db = JFactory::getDBO();
		
		foreach($idToDelete as $id) {
			$db->setQuery("DELETE FROM #__easiv2_instances WHERE id='".$id."'");
			if($db->query() != false) {
				echo "<img src=\"components/com_easiv2/images/true.png\" /> ".JText::_('COM_EASIV2_MANAGEINSTANCES_CONTROLLER_DELETEDSUCCESSFULLY_PART1')." <b>".$id."</b> ".JText::_('COM_EASIV2_MANAGEINSTANCES_CONTROLLER_DELETEDSUCCESSFULLY_PART2')."<br>";
			}
		}		
		
		Jrequest::setVar('view', 'manageInstances');
		parent::display();
	}
}